import Window from '@ohos.window';
import type Common from '@ohos.app.ability.common';

type BaseContext = Common.BaseContext

export class WindowManager {
  private static instance: WindowManager;
  private window1: Window.Window = null
  private window2: Window.Window = null

  private constructor() {
  }

  public static getInstance(): WindowManager {
    if (!WindowManager.instance) {
      WindowManager.instance = new WindowManager()
    }
    return WindowManager.instance
  }

  async showWindow2(context: BaseContext) {
    if (!this.window2) {
      this.window2 = await this.createWindow2(context)
    }
    await this.window2.show();
  }

  async hideWindow2() {
    if (this.window2) {
      await this.window2.hide();
    }
  }

  private async createWindow2(context: BaseContext): Promise<Window.Window> {
    let winHandle = await Window.create(context, "DATA", Window.WindowType.TYPE_FLOAT);
    await winHandle.moveTo(0, 0);
    await winHandle.resetSize(1920, 1200);
    await winHandle.loadContent('pages/Home');
    return winHandle;
  }

  private async createWindow1(context: BaseContext): Promise<Window.Window> {
    let winHandle = await Window.create(context, "INIT", Window.WindowType.TYPE_FLOAT);
    const w = 900
    await winHandle.moveTo((1920 -w)/2, (1200 -w)/2);
    await winHandle.resetSize(w, w);
    await winHandle.loadContent('pages/Index');
    return winHandle;
  }

  async showWindow1(context: BaseContext) {
    if (!this.window1) {
      this.window1 = await this.createWindow1(context)
    }
    await this.window1.show();
  }

  async hideWindow1() {
    if (this.window1) {
      await this.window1.hide();
    }
  }

}